<template>
  <h1>药品登记入库</h1>
  <el-form ref="ruleFormRef" style="
      max-width: 600px;
      justify-content: center;
      margin-left: 25%;
      margin-top: 5%;
    " :model="ruleForm" :rules="rules" label-width="auto" class="demo-ruleForm" :size="formSize" status-icon>
    <el-form-item label="药品名称" prop="name">
      <el-input v-model="ruleForm.name" />
    </el-form-item>

    <el-form-item label="药品单价" prop="price">
      <el-input v-model="ruleForm.price" />
    </el-form-item>

    <el-form-item label="药品编号" prop="number">
      <el-input v-model.number="ruleForm.number" />
    </el-form-item>

    <el-form-item label="药品供应商" prop="medcomoany">
      <el-input v-model="ruleForm.medcomoany" />
    </el-form-item>

    <el-form-item label="有效日期" required>
      <el-col :span="11">
        <el-form-item prop="date1">
          <el-date-picker v-model="ruleForm.date1" type="date" aria-label="Pick a date" placeholder="Pick a date"
            style="width: 100%" />
        </el-form-item>
      </el-col>
      <el-col class="text-center" :span="2">
        <span class="text-gray-500">-</span>
      </el-col>
      <el-col :span="11">
        <el-form-item prop="date2">
          <el-date-picker v-model="ruleForm.date2" aria-label="Pick a date" placeholder="Pick a date"
            style="width: 100%" />
        </el-form-item>
      </el-col>
    </el-form-item>

    <el-form-item label="药品简介" prop="desc">
      <el-input v-model="ruleForm.desc" type="textarea" />
    </el-form-item>

    <el-form-item style="margin-left: 48%; margin-top: 5%">
      <el-button type="primary" @click="submitForm(ruleFormRef)">
        Create
      </el-button>
      <el-button @click="resetForm(ruleFormRef)">Reset</el-button>
    </el-form-item>
  </el-form>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import type { ComponentSize, FormInstance, FormRules } from 'element-plus'
import service from '@/request'
// import { tr } from 'element-plus/es/locale'

interface RuleForm {
  name: string
  number: string
  price: number
  medcomoany: string
  date1: string
  date2: string
  desc: string
}

const formSize = ref<ComponentSize>('default')
const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive<RuleForm>({
  name: '',
  number: '',
  price: 0,
  medcomoany: '',
  date1: '',
  date2: '',
  desc: '',
})

const rules = reactive<FormRules<RuleForm>>({
  name: [
    { required: true, message: 'Please input meidicine name', trigger: 'blur' },
    // { min: 3, max: 5, message: 'Length should be 3 to 5', trigger: 'blur' },
  ],
  number: [
    {
      required: true,
      message: 'Please enter medicine number',
      trigger: 'change',
    },
  ],
  price: [
    {
      required: true,
      message: 'Please enter medicine Unit Price',
      trigger: 'change',
    },
  ],
  medcomoany: [
    {
      required: true,
      message: 'Please enter the supplier of the medicsation',
      trigger: 'change',
    },
  ],
  date1: [
    {
      type: 'date',
      required: true,
      message: 'Please pick a date',
      trigger: 'change',
    },
  ],
  date2: [
    {
      type: 'date',
      required: true,
      message: 'Please pick a time',
      trigger: 'change',
    },
  ],
  desc: [
    { required: true, message: 'Please input activity form', trigger: 'blur' },
  ],
})

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      console.log('submit!')
      try {
        const response = await service.post("/Medicine/login",
          {
            "medname": ruleForm.name,
            "medprice": ruleForm.price,
            "mednumber": ruleForm.number,
            "medcompany": ruleForm.medcomoany,
            "medprofile": ruleForm.desc,
            "productiontime":ruleForm.date1,
            "effectivetime":ruleForm.date2
          }
        );
        // tableData.value = response.data;
        console.log(response.data);
        alert("录入成功");
      } catch (error) {
        alert("输入有误");
      }
    } else {
      console.log('error submit!', fields)
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}

// const options = Array.from({ length: 10000 }).map((_, idx) => ({
//   value: `${idx + 1}`,
//   label: `${idx + 1}`,
// }))
</script>